package com.jy.novel.controller.system;

import com.jy.novel.common.Page;
import com.jy.novel.controller.base.BaseController;
import com.jy.novel.common.CommonResult;
import com.jy.novel.pojo.form.MenuAddForm;
import com.jy.novel.pojo.form.MenuEditForm;
import com.jy.novel.pojo.system.MenuInfo;
import com.jy.novel.service.system.MenuService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by jihb on 4/13/16.
 */
@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Autowired
    MenuService menuService;

    @RequestMapping(value = "/manager",method = RequestMethod.GET)
    public String menuManager(){
        return "system/menu_manager";
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult menuList(
        @RequestParam(value = "parentId",required = false,defaultValue = "0")String parentId,
        @RequestParam(value = "level",required = false,defaultValue = "1")String level
    ) {
        CommonResult<List<MenuInfo>> listCommonResult = menuService.findMenuList(NumberUtils.toLong(parentId),NumberUtils.toInt(level));
        return listCommonResult;
    }

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public Page<MenuInfo> menuPage(
            @RequestParam(value = "parentId",required = false)String parentId,
            @RequestParam(value = "start",required = false,defaultValue = "1")String start,
            @RequestParam(value = "limit",required = false,defaultValue = "20")String limit,
            @RequestParam(value = "level",required = false,defaultValue = "1")String level
    ) {
        int curPage = 1;
        int pageSize = 20;
        curPage = NumberUtils.toInt(start,0)/(NumberUtils.toInt(limit,20)+1);
        pageSize = NumberUtils.toInt(limit,20);
        CommonResult<Page<MenuInfo>> pageCommonResult = menuService.findMenuPage(NumberUtils.toLong(parentId),NumberUtils.toInt(level),curPage,pageSize);
        return pageCommonResult.getResult();
    }

    @RequestMapping(value = "/tree",method = RequestMethod.GET)
    @ResponseBody
    public Object menuTree(
            @RequestParam(value = "parentId",required = false)String parentId,
            @RequestParam(value = "level",required = false,defaultValue = "2")String level
    ) {
        if (StringUtils.isBlank(parentId)){
            return null;
        }
        CommonResult<List<MenuInfo>> listCommonResult = menuService.findMenuList(NumberUtils.toLong(parentId),NumberUtils.toInt(level,1));
        return listCommonResult.getResult();
    }

    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    @ResponseBody
    public Object menuDetail(
            @RequestParam(value = "id")String id
    ){
        CommonResult<MenuInfo> menuInfoCommonResult = menuService.findMenuInfo(NumberUtils.toLong(id));
        return menuInfoCommonResult.getResult();
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addMenu(
            @ModelAttribute MenuAddForm addForm
            ){
        if (addForm == null){
            return returnFailure("缺少参数");
        }
        if (StringUtils.isBlank(addForm.getName())){
            return returnFailure("缺少菜单名称");
        }
        if (addForm.getLevel() == null){
            return returnFailure("缺少菜单层级");
        }
        int level = addForm.getLevel();
        if ((level == 2 || level == 3) && StringUtils.isBlank(addForm.getPath())){
            return returnFailure("缺少菜单访问路径");
        }
        CommonResult<Boolean> commonResult = menuService.addMenuInfo(addForm);
        return returnBoolWrap(commonResult.getCode()==0, commonResult.getMessage());
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateMenu(
            @RequestParam(value = "id")String id,
            @RequestParam(value = "level")String level,
            @ModelAttribute MenuEditForm editForm
    ){
        if (editForm == null){
            return returnFailure("缺少参数");
        }
        if (StringUtils.isBlank(editForm.getName())){
            return returnFailure("缺少菜单名称");
        }
        if (level == null){
            return returnFailure("缺少菜单层级");
        }
        int levelVal = NumberUtils.toInt(level,1);
        if ((levelVal == 2 || levelVal == 3) && StringUtils.isBlank(editForm.getPath())){
            return returnFailure("缺少菜单访问路径");
        }
        CommonResult<Boolean> commonResult = menuService.updateMenuInfo(editForm,NumberUtils.toLong(id));
        return returnBoolWrap(commonResult.getCode()==0, commonResult.getMessage());
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteMenu(
            @RequestParam(value = "id")String id
    ){
        CommonResult<Boolean> commonResult = menuService.deleteMenuInfo(NumberUtils.toLong(id));
        return returnBoolWrap(commonResult.getCode()==0, commonResult.getMessage());
    }
}
